import {createRouter, createWebHistory} from 'vue-router'
import { Toast } from 'vant';
import store from '../store';
const routes = [{
    path: '',
    name: 'DefaultHome',
    component: () => import ('../views/home/Home'),
    meta: {
        title: '图书兄弟-首页'
    }
},
    {
        path: '/home',
        name: 'Home',
        component: () => import ('../views/home/Home'),
        meta: {
            title: '图书兄弟-首页'
        }
    },
    {
        path: '/category',
        name: 'Category',
        component: () => import ('../views/category/Category'),
        meta: {
            title: '图书兄弟-分类'
        }
    },
    {
        path: '/detail',
        name: 'Detail',
        component: () => import ('../views/detail/Detail'),
        meta: {
            title: '图书兄弟-详情'
        }
    },
    {
        path: '/profile',
        name: 'Profile',
        component: () => import ('../views/profile/Profile'),
        meta: {
            title: '图书兄弟-个人中心'
        }
    },
    {
        path: '/shopcart',
        name: 'Shopcart',
        component: () => import ('views/shopcart/Shopcart'),
        meta: {
            title: '图书兄弟-购物车',
            isAuthRequired:true
        }
    },
    {
        path: '/register',
        name: 'Register',
        component: () => import ('views/profile/Register'),
        meta: {
            title: '用户注册'
        }
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import ('views/profile/Login'),
        meta: {
            title: '用户注册'
        }
    },
    {
        path: '/address',
        name: 'Address',
        component: () => import ('views/profile/Address'),
        meta: {
            title: '地址管理',
            isAuthRequired:true
        }
    },
    {
        path: '/addressedit',
        name: 'AddressEdit',
        component: () => import ('views/profile/AddressEdit'),
        meta: {
            title: '添加地址',
            isAuthRequired:true
        }
    },
    {
        path: '/collect',
        name: 'Collect',
        component: () => import ('views/profile/Collect'),
        meta: {
            title: '我的收藏',
            isAuthRequired:true
        }
    },
    {
        path: '/setting',
        name: 'Setting',
        component: () => import ('views/profile/Setting'),
        meta: {
            title: '账号设置',
            isAuthRequired:true
        }
    },
    {
        path: '/about',
        name: 'About',
        component: () => import ('views/profile/About'),
        meta: {
            title: '关于我们',
            isAuthRequired:true
        }
    },
    {
        path: '/order',
        name: 'Order',
        component: () => import ('views/order/Order'),
        meta: {
            title: '订单列表',
            isAuthRequired:true
        }
    },
    {
        path: '/createorder',
        name: 'CreateOrder',
        component: () => import ('views/order/CreateOrder'),
        meta: {
            title: '订单预览',
            isAuthRequired:true
        }
    },
    {
        path: '/orderdetail',
        name: 'OrderDetail',
        component: () => import ('views/order/OrderDetail'),
        meta: {
            title: '订单预览',
            isAuthRequired:true
        }
    },
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

router.beforeEach((to, from, next) => {
    // 如果没有登录，在这里跳转login
    if(to.meta.isAuthRequired && store.state.user.isLogin === false) {
        Toast.fail('您还没有登录， 请先登录');
        return next('/login');
    }else{
        next();
    }
    document.title = to.meta.title;
})

export default router